package com.example.controller;

import com.example.entity.auth.Forget;
import com.example.entity.auth.Register;
import com.example.entity.RestBean;
import com.example.service.AuthorizeService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Pattern;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

@Validated
@RestController
@RequestMapping("/api/auth")
public class AuthorizeController {

//    private final String EMAIL_REGEX = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
    private final String USERNAME_REGEX = "^[a-zA-Z0-9\\u4e00-\\u9fa5]+$";

    @Resource
    AuthorizeService service;

    @GetMapping ("/check-username")
    public boolean checkUsername(@RequestParam("username") String username){
       return service.checkUsernameExist(username);
    }

    @PostMapping("/valid-register-email")
    public RestBean<String> validateRegisterEmail(@Email @RequestParam("email") String encodedEmail,
                                          HttpSession httpSession){

        try {
            String email = URLDecoder.decode(encodedEmail, StandardCharsets.UTF_8.name());
            String s =service.sendValidateEmail(email,httpSession.getId(),false);
            if ( s == null ){
                return RestBean.success("验证码已发送，请注意邮件");
            } else {
                return RestBean.failure(400,s);
            }
        } catch (UnsupportedEncodingException e) {
            // 处理解码异常
            return RestBean.failure(500, "内部服务器错误");
        }
    }

    @PostMapping("/valid-reset-email")
    public RestBean<String> validateResetEmail(@Email @RequestParam("email") String encodedEmail,
                                          HttpSession httpSession){

        try {
            String email = URLDecoder.decode(encodedEmail, StandardCharsets.UTF_8.name());
            String s =service.sendValidateEmail(email,httpSession.getId(),true);
            if ( s == null ){
                return RestBean.success("验证码已发送，请注意邮件");
            } else {
                return RestBean.failure(400,s);
            }
        } catch (UnsupportedEncodingException e) {
            // 处理解码异常
            return RestBean.failure(500, "内部服务器错误");
        }
    }

    @PostMapping("/register")
    public RestBean<String> registerUser(@Pattern(regexp = USERNAME_REGEX) @RequestParam("username") String username,
                                         @RequestParam("password") String password,
                                         @Email @RequestParam("email") String email,
                                         @RequestParam("code") String code,
                                         HttpSession httpSession){
        Register register = new Register(username,password,email,code);
        String s = service.validateAndRegister(register,httpSession.getId());
        if (s == null){
            return RestBean.success("注册成功");
        }else {
                return RestBean.failure(400,s);
        }
    }

    @PostMapping("/reset-password")
    public RestBean<String> forgetPassword(@RequestParam("password") String password,
                                           @Email @RequestParam("email") String email,
                                           @RequestParam("code") String code,
                                           HttpSession httpSession){
        Forget forget = new Forget(password,email,code);
        String s = service.validateAndResetPassword(forget,httpSession.getId());
        if (s == null){
            return RestBean.success("重置密码成功");
        }else {
            return RestBean.failure(400,s);
        }

    }
}
